package com.liu.demo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
/**
 *功能描述  配置Druid连接池
 * @author QS.L
 * @date 2020/5/16
 * @param
 * @return
 */
@Configuration
public class DruidConfig {


    @Bean(value = "druidDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }


    @Bean(value = "statViewServlet")
    //后台监控:查看执行的语句 时间 结果。。。
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //后台登录 需要账号密码
        HashMap<String,String> InitParameters = new HashMap();
        //增加配置
        InitParameters.put("loginUsername","root");
        InitParameters.put("loginPassword","1234");
        //允许谁可以访问 //禁止谁可以访问
        //InitParameters.put("allow","");
        bean.setInitParameters(InitParameters);
       return bean;
    }

    @Bean
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean = new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> map = new HashMap<>();
        map.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(map);
        return bean;
    }
}
